<template>
  <div class="log-in-container">
    <div class="header"></div>
    <div class="container">
      <div class="form">
        <h2>注册</h2>
        <el-form
            ref="form"
            :model="user"
            hide-required-asterisk
            :rules="rules"
        >
          <el-form-item
              prop="username"
          >
            <el-input v-model="user.username" placeholder="账号"/>
          </el-form-item>
          <el-form-item
              prop="password"
          >
            <el-input v-model="user.password" placeholder="密码"/>
          </el-form-item>
        </el-form>
        <button
            type="submit"
            class="btn"
            @click="login()"
        >注册</button>
      </div>
    </div>
  </div>
</template>

<script>
import api from '@/api'

export default {
    name: 'Register',
    data() {
        return {
            user: {
                username: '',
                password: ''
            },
            rules: {
                password: [{pattern: /^[a-zA-Z0-9]{6,20}$/, message: '密码只能为大小写字母或数字，且长度为6~20个字符', trigger: 'blur'}]
            }
        }
    },
    methods: {
        async login() {
            const resp = await api.login.register(this.user)
            console.log(resp)
            if(resp.code === 200) {
                this.$notify.success({
                    title: '成功',
                    message: '注册成功'
                })
                await this.$router.push({name: 'login'})
            } else if(resp.Code === 1401){
                this.$notify.error({
                    title: '错误',
                    message: '该账号已存在！'
                })
            } else {
                this.$notify.error({
                    title: '失败',
                    message: '未知错误！'
                })
            }
        }
    }
}
</script>

<style scoped lang="less">
.log-in-container {
  background-color: #1fc8db;
  background-image: linear-gradient(141deg,#9fb8ad 0%,#1fc8db 51%,#2cb5e8 75%);
  background-size: cover;
  background-position: center;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  position: relative;
  width: 400px;
  min-height: 400px;
  background: rgba(255, 255, 255, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(5px);
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-right: 1px solid rgba(255, 255, 255, 0.2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.form {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 30px;
}

.form h2 {
  position: relative;
  color: #fff;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 5px;
  margin-bottom: 30px;
  cursor: pointer;
}

.form h2::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: -10px;
  width: 0px;
  height: 3px;
  background: #fff;
  transition: 0.5s;
}

.form h2:hover:before {
  width: 53px;
}

.form .el-form-item{
  width: 100%;
  margin-top: 20px;
}

.form .btn {
  background: #fff;
  color: #666;
  max-width: 100px;
  margin-bottom: 10px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  margin-top: 10px;
}
</style>